//========================================================================
/**
 * Author   : cuisw <shaovie@gmail.com>
 * Date     : 2008-10-24 18:01
 */
//========================================================================

#ifndef _PROACTORIMPL_H_
#define _PROACTORIMPL_H_
#include "Pre.h"

#include "AsynchIO.h"

/**
 * @class ProactorImpl
 *
 * @brief A manager for asynchronous event demultiplexing. This class
 * is the base class for all the concrete implementation classes.
 */
class ProactorImpl
{
public:
    // Virtual destruction.
    virtual ~ProactorImpl (void);

    // Close the IO completion port.
    virtual int close (void) = 0;

    // This method adds the @a handle to the I/O completion port. 
    virtual int register_handle (NDK_HANDLE handle,
	    const void *completion_key,
	    int event_mask) = 0;

    // Run the event loop until the <handle_events> method returns -1 
    // or the <end_event_loop> method is invoked.
    virtual int run_event_loop (void) = 0;

    // Terminate event loop. 
    virtual void end_event_loop (void) = 0;

    // Report if the Proactor event loop is finished.
    virtual int event_loop_done (void) = 0;

    /**
     * Dispatch a single set of events, waiting up to a specified time limit
     * if necessary. wait_time the time to wait for an event to occur. 
     * This is a relative time. 
     * Returns  0 if no events occur before the wait_time expires.
     * Returns >1 is the number of completions are dispatched. 
     * On error, returns -1
     */
    virtual int handle_events (const TimeValue *wait_time) = 0;

    // Get number of thread used by proactor.
    virtual size_t number_of_threads (void) const = 0;

    // Set number of thread used by proactor.
    virtual void number_of_threads (size_t threads) = 0;

    // = Factory methods for the operations

    // Create the correct implementation class for doing AsynchReadStream.
    virtual AsynchReadStreamImpl *create_asynch_read_stream (void) = 0;
    virtual AsynchReadStreamResultImpl *create_asynch_read_stream_result (
	    const AsynchHandler *handler,
	    NDK_HANDLE handle, 
	    MessageBlock *mb,
	    size_t bytes_to_read) = 0;

    //
    virtual AsynchWriteStreamImpl *create_asynch_write_stream (void) = 0;
    virtual AsynchWriteStreamResultImpl *create_asynch_write_stream_result (
	    const AsynchHandler *handler,
	    NDK_HANDLE handle,
	    MessageBlock *mb,
	    size_t bytes_to_write) = 0;

    //
    virtual AsynchAcceptImpl *create_asynch_accept (void) = 0;
    virtual AsynchAcceptResultImpl *create_asynch_accept_result (
	    const AsynchHandler *handler,
	    NDK_HANDLE listen_handle,
	    MessageBlock *mb,
	    size_t num_to_accept) = 0;
};

#include "Post.h"
#endif

